<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_indexation=new indexation();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT"){
		if(isset($_POST["code_index"])){$_indexation->set_code_index($_POST["code_index"]);}
		if(isset($_POST["code_ue"])){$_indexation->set_code_ue($_POST["code_ue"]);}
		if(isset($_POST["titre"])){$_indexation->set_titre($_POST["titre"]);}
		if(isset($_POST["discipline"])){$_indexation->set_discipline($_POST["discipline"]);}
		if(isset($_POST["mots_cles"])){$_indexation->set_mots_cles($_POST["mots_cles"]);}
		if(isset($_POST["date_creation"])){$_indexation->set_date_creation($_POST["date_creation"]);}
		if(isset($_POST["public_cible"])){$_indexation->set_public_cible($_POST["public_cible"]);}
		if(isset($_POST["objectif"])){$_indexation->set_objectif($_POST["objectif"]);}
		if(isset($_POST["nature"])){$_indexation->set_nature($_POST["nature"]);}
		if(isset($_POST["description"])){$_indexation->set_description($_POST["description"]);}
		$res=$_indexation->INSERT(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE"){
		if(isset($_POST["code_index"])){
		$_indexation->set_code_index($_POST["code_index"]);
		$res=$_indexation->DELETE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}
elseif($_POST["operation"]=="UPDATE"){
		if(isset($_POST["code_index"])){$_indexation->set_code_index($_POST["code_index"]);}
		if(isset($_POST["code_ue"])){$_indexation->set_code_ue($_POST["code_ue"]);}
		if(isset($_POST["titre"])){$_indexation->set_titre($_POST["titre"]);}
		if(isset($_POST["discipline"])){$_indexation->set_discipline($_POST["discipline"]);}
		if(isset($_POST["mots_cles"])){$_indexation->set_mots_cles($_POST["mots_cles"]);}
		if(isset($_POST["date_creation"])){$_indexation->set_date_creation($_POST["date_creation"]);}
		if(isset($_POST["public_cible"])){$_indexation->set_public_cible($_POST["public_cible"]);}
		if(isset($_POST["objectif"])){$_indexation->set_objectif($_POST["objectif"]);}
		if(isset($_POST["nature"])){$_indexation->set_nature($_POST["nature"]);}
		if(isset($_POST["description"])){$_indexation->set_description($_POST["description"]);}
		$res=$_indexation->UPDATE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["code_index"]) && !empty($_POST["code_index"])){
		$constraint.=" AND code_index='".$_POST["code_index"]."'";
	}

	if(isset($_POST["code_ue"]) && !empty($_POST["code_ue"])){
		$constraint.=" AND code_ue='".$_POST["code_ue"]."'";
	}

	if(isset($_POST["titre"]) && !empty($_POST["titre"])){
		$constraint.=" AND titre='".$_POST["titre"]."'";
	}

	if(isset($_POST["discipline"]) && !empty($_POST["discipline"])){
		$constraint.=" AND discipline='".$_POST["discipline"]."'";
	}

	if(isset($_POST["mots_cles"]) && !empty($_POST["mots_cles"])){
		$constraint.=" AND mots_cles='".$_POST["mots_cles"]."'";
	}

	if(isset($_POST["date_creation"]) && !empty($_POST["date_creation"])){
		$constraint.=" AND date_creation='".$_POST["date_creation"]."'";
	}

	if(isset($_POST["public_cible"]) && !empty($_POST["public_cible"])){
		$constraint.=" AND public_cible='".$_POST["public_cible"]."'";
	}

	if(isset($_POST["objectif"]) && !empty($_POST["objectif"])){
		$constraint.=" AND objectif='".$_POST["objectif"]."'";
	}

	if(isset($_POST["nature"]) && !empty($_POST["nature"])){
		$constraint.=" AND nature='".$_POST["nature"]."'";
	}

	if(isset($_POST["description"]) && !empty($_POST["description"])){
		$constraint.=" AND description='".$_POST["description"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_indexation->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_indexation->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>